import { createSlice } from '@reduxjs/toolkit'

export const info = createSlice({
    name: 'info',
    initialState: {
        email: '',
        userId: '',
        userName: '',
        role: ''
    },
    reducers: {
        setinfo(state, action) {
            state.email = action.payload.email
            state.userId = action.payload.userId
            state.userName = action.payload.userName
            state.role = action.payload.role || ''
        },
        initUserInfo(state) {
            // 从 localStorage 恢复用户信息
            const email = localStorage.getItem('email') || ''
            const userId = localStorage.getItem('userId') || ''
            const userName = localStorage.getItem('userName') || ''
            const role = localStorage.getItem('userRole') || ''
            
            state.email = email
            state.userId = userId
            state.userName = userName
            state.role = role
        }
    },
})

// Action creators are generated for each case reducer function
export const { setinfo, initUserInfo } = info.actions

export default info.reducer